package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.time.LocalDateTime;
import java.time.Year;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;
    /**
     * 新增分类
     *
     * @param categoryDTO
     * @return
     */
    @Override
    public Result addCategory(CategoryDTO categoryDTO) {

        //1.判断非空
        if (categoryDTO.getName() == null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }

//        //2.判断是否已有此分类,如果存在则返回错误信息
//        if (categoryDTO.getName() != null){
//            return Result.error(MessageConstant.ALREADY_EXISTS_CATEGORY);
//        }

        //没有此分类创建对象
        Category category = new Category();

        //3.调用mapper层添加数据

        //复制CategoryDTO给category
        BeanUtils.copyProperties(categoryDTO,category);

        //添加状态
        category.setStatus(StatusConstant.ENABLE);
        //添加创建时间
        category.setCreateTime(LocalDateTime.now());
        //添加更新时间
        category.setUpdateTime(LocalDateTime.now());
        //添加创建人
        category.setCreateUser(BaseContext.getCurrentId());
        //添加修改人
        category.setUpdateUser(BaseContext.getCurrentId());

        //调用mapper层,把属性传入给mapper
        categoryMapper.insert(category);

        //4.返回数据
        return Result.success();
    }

    /**
     * 分页查询
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public Result pageCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        //1.参数校验
        //判断分页是否为空
        if (categoryPageQueryDTO.getPage() == null){
            categoryPageQueryDTO.setPage(1);
        }

        if (categoryPageQueryDTO.getPageSize() == null){
            categoryPageQueryDTO.setPageSize(5);
        }

        //2.开启分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        //3.调用mapper
        Page pageParam = categoryMapper.pageCategory(categoryPageQueryDTO);

        //4.返回数据
        PageResult pageResult = new PageResult();

        pageResult.setRecords(pageParam.getResult());

        pageResult.setTotal(pageParam.getTotal());

        return Result.success(pageResult);
    }

    /**
     * 根据id删除分类
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteCategory(Long id) {
        //1.参数校验
        if (id == null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //调用mapper
        categoryMapper.deleteCategory(id);

        //返回数据
        return Result.success();
    }

    /**
     * 修改分类
     *
     * @param category
     * @return
     */
    @Override
    public Result updateCategory(Category category) {
        //1.参数校验
        if (category == null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        //2.调用mapper,传参
        Category category1 = new Category();
        BeanUtils.copyProperties(category,category1);

        //3.修改时间,修改人
        category1.setUpdateTime(LocalDateTime.now());
        category1.setUpdateUser(BaseContext.getCurrentId());

        //4.返回数据
        categoryMapper.updateCategory(category1);

        return Result.success();
    }

    /**
     * 启动/禁用分类
     *
     * @param status
     * @param id
     * @return
     */
    @Override
    public Result updateStatus(Integer status, Long id) {
        //1.参数校验
        if (id == null){
            return Result.error(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //2.调用mapper
        Category category = new Category();
        category.setStatus(status);
        category.setId(id);
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());

        //3.返回数据
        categoryMapper.updateStatus(category);

        return Result.success();
    }

    /**
     * 按照类型查找
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> selectType(Integer type) {
        return categoryMapper.selectType(type);
    }

}
